package com.sports.controller;

import com.sports.entity.Admin;
import com.sports.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: AdminController
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author WGH
 * @Create 2024/3/26 21:16
 * @Version 1.0
 */
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    AdminService adminService;
    
    @PostMapping("addAdmin")
    public Map  addAdmin(String password1,@RequestBody Admin admin){
        Map map = new HashMap();
        if (!password1.equals(admin.getPassword())){
            map.put("message","not-same");
            return map;
        }
        adminService.save(admin);
        map.put("message","success");
        return map;
    }

    @DeleteMapping("deleteAdmin/{adminId}")
    public Map deleteAdmin(@PathVariable("adminId") Integer adminId){
        Map map = new HashMap();
        boolean removeById = adminService.removeById(adminId);
        if (!removeById) {
            map.put("message","nothing");
            return map;
        }
        map.put("message","success");
        return map;
    }

    @PutMapping("updateAdmin")
    public Map updateAdmin(@RequestBody Admin admin){
        Map map = new HashMap();
        boolean updateById = adminService.updateById(admin);
        if (!updateById) {
            map.put("message","nothing");
            return map;
        }
        map.put("message","success");
        return map;
    }

    @GetMapping("getAdmin/{adminId}")
    public Map getAdmin(@PathVariable("adminId") Integer adminId){
        Map map = new HashMap();
        Admin admin = adminService.getById(adminId);
        if (admin == null) {
            map.put("message","nothing");
            return map;
        }
        map.put("message","success");
        map.put("admin",admin);
        return map;
    }

    @GetMapping("getAllAdmin")
    public Map getAllAdmin(){
        Map map = new HashMap();
        map.put("message","success");
        map.put("admin",adminService.list());
        return map;
    }

}